package com.itheima.service.impl;

import com.itheima.mapper.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;

    /**
     * 下载预约设置模板
     */
    @Override
    public void downloadModel(HttpServletResponse response) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("预约设置模板");
        XSSFRow row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("日期");
        row0.createCell(1).setCellValue("可预约人数");
        response.setHeader("Content-Disposition", "attachment;filename=model.xlsx");
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();


    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        ordersettingDao.editNumberByOrderDate(orderSetting);
    }

    /**
     * 根据日期获取预约设置数据
     * @param month
     * @return
     */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(String month) {
        // 解析传入的日期字符串为 YearMonth 对象
        YearMonth yearMonth = YearMonth.parse(month, DateTimeFormatter.ofPattern("yyyy-M"));

        // 获取该月的第一天和最后一天
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
        return ordersettingDao.getOrderSettingByMonth(firstDayOfMonth, lastDayOfMonth);
    }

    /**
     * 批量导入预约设置数据
     * @param excelFile
     */
    @Override
    public void upload(MultipartFile excelFile) throws IOException, ParseException {
        InputStream inputStream = excelFile.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            XSSFRow row = sheet.getRow(i);
            Date date = row.getCell(0).getDateCellValue();
            int number = (int) row.getCell(1).getNumericCellValue();
            OrderSetting orderSetting = new OrderSetting(date, number);
            orderSetting.setReservations(0);
            ordersettingDao.addOrderSetting(orderSetting);
        }
    }
}
